Implement a function clone to replicate values for 5 major data types in JavaScript, including number, String, Object, Array, Boolean.1 /** Object Cloning2 * Supports basic data types and objects3 * Recursive method*/ 4 functionClone (obj) {5
This article mainly introduces how to clone objects in javascript. If you need a friend, you can refer to the reference relationship between objects under development and interrupt them, but only the next copy is everywhere, it is inevitable to
Reprint: https://www.cnblogs.com/zouhao/p/7278117.htmlA: In JavaScript, if the cloned object is a basic type, we can assign it directly:1 var sStr = "Kingwell"; 2 var cStr = sstr;3 alert (cStr);//output KINGWELL4 SSTR = "ABC"; 5 alert
Clone is to copy the original thing as it is, and the new copy has nothing to do with the previous thing.A: In JavaScript, if the cloned object is a basic type, we can assign it directly:var sStr = "Kingwell"; var cStr = sStr; alert (CSTR); //
In-depth understanding of Object Clone and javascriptclone in JavaScript
JavaScript does not directly provide the Object Clone method. Therefore, when object B is changed in the following code, object a is changed.
A = {k1: 1, k2: 2, k3: 3 };
B =;
B.
I. In Javascript, if the cloned object is of the basic type, we can assign a value directly:
1 var sStr = "kingwell"; 2 var cStr = sStr; 3 alert (cStr); // output kingwell4 sStr = "abc"; 5 alert (cStr ); // output kingwell;
When a value is assigned
JavaScript does not directly provide a method for object replication (the object clone). So when you change object B in the following code, you change object A.
A = {k1:1, k2:2, k3:3};
b = A;
B.K2 = 4;
If you just want to change B and keep a
Development, interrupt the reference relationship between objects, just want the next copy of the situation everywhere, clone an object is inevitable.
In JavaScript, the simple way is to use the JSON function, stringify the object into a string,
As javascript arrays are widely used and more friends are using them, students have compiled some js data learning materials for everyone. After reading them, they should have an understanding of arrays. In JavaScript, you can use new Array to
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.